package com.snail.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.model.pojo.BizQuestion;
import com.snail.model.pojo.BizUserQuestionCollect;
import com.snail.service.BizQuestionCollectService;
import com.snail.model.pojo.BizQuestionCollect;
import com.snail.mapper.BizQuestionCollectMapper;
import com.snail.service.BizUserQuestionCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Monica
* @description 针对表【biz_question_collect(用户收藏题表)】的数据库操作Service实现
* @createDate 2025-07-16 10:22:25
*/
@Service
public class BizQuestionCollectServiceImpl extends ServiceImpl<BizQuestionCollectMapper, BizQuestionCollect>
    implements BizQuestionCollectService {
    @Autowired
    BizQuestionCollectMapper questionCollectMapper;
    @Override
    public boolean addQuestionToCollect(BizQuestionCollect collect) {
        // 避免重复收藏
        boolean exists = lambdaQuery()
                .eq(BizQuestionCollect::getQuestionId, collect.getQuestionId())
                .eq(BizQuestionCollect::getCollectId, collect.getCollectId())
                .exists();
        System.out.println("是否已经收藏了"+exists);
        if (exists) {
            throw new RuntimeException("该题目已在收藏夹中");
        }

        return this.save(collect);
    }

    @Override
    public boolean removeQuestionFromCollect(Integer questionId, Integer collectId) {
        return this.lambdaUpdate()
                .eq(BizQuestionCollect::getQuestionId, questionId)
                .eq(BizQuestionCollect::getCollectId, collectId)
                .remove();
    }


    public List<BizQuestion> getQuestionsInCollect(Integer collectId) {
        return questionCollectMapper.getQuestionsByCollectId(collectId);
    }
}




